package cn.taobaoke.restful.controller;

import cn.taobaoke.restful.domain.NoticeEntity;
import cn.taobaoke.restful.service.NoticeService;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * NoticeController类.
 * +-----------------------------------------------------------------------------------------------+
 * |类名称: NoticeController                                                                         |
 * |创建时间: 5/6/17                                                                               |
 * |功能简介:                                                                                       |
 * |参考链接:                                                                                       |
 * +-----------------------------------------------------------------------------------------------+
 *
 * @author luch
 */
@RestController
@RequestMapping("/{domain}/{domainId}/api/notices")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class NoticeController extends BaseController {

    @GetMapping("")
    public Result getAll(@PathVariable Long domainId) {
        logger.debug("GET /{}/api/notices", domainId);

        List<NoticeEntity> notices = noticeService.getAll(domainId);
        return wrapObj(notices);
    }

    @PostMapping("")
    public Result save(@PathVariable Long domainId, @RequestBody NoticeEntity noticeEntity) {
        logger.debug("POST /{}/api/notices", domainId);

        noticeEntity = noticeService.save(noticeEntity, domainId);
        return wrapObj(noticeEntity);
    }

    @DeleteMapping("/{noticeId}")
    public Result delete(@PathVariable Long domainId, @PathVariable Long noticeId) {
        logger.debug("DELETE /{}/api/notices/{}", domainId, noticeId);

        noticeService.delete(noticeId, domainId);
        return wrapVoid();
    }

    private final @NonNull
    NoticeService noticeService;

    private static Logger logger = LogManager.getLogger(NoticeController.class);
}
